package com.scaffold.authority.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import wangjg.commons.page.Page;

import com.scaffold.authority.entity.RoleAuthorityRelation;

@Repository
public class RoleAuthorityRelationDAO extends BasicBizDAO<RoleAuthorityRelation,Long>{

	public List<RoleAuthorityRelation> findByRoleId(Long roleId) {
		return this.findListByProperty("roleId", roleId);
	}
	public List<RoleAuthorityRelation> findByAuthorityId(Long authorityId) {
		return this.findListByProperty("authorityId", authorityId);
	}

	public List<RoleAuthorityRelation> pageQuery(Page page , Long roleId, Long authorityId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		if(roleId != null){
			criteria.add(Restrictions.eq("roleId", roleId));
		}
		if(authorityId != null){
			criteria.add(Restrictions.eq("authorityId", authorityId));
		}
		criteria.addOrder(Order.desc("id"));
		return this.findByCriteria(criteria, page);
	}

	public RoleAuthorityRelation findItem(Long roleId, Long authorityId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(this.persistentClass);
		criteria.add(Restrictions.eq("roleId", roleId));
		criteria.add(Restrictions.eq("authorityId", authorityId));
		return this.findUniqueByCriteria(criteria);
	}
}
